package com.ayoka.parenting.adapter;

import android.widget.ImageView;
import android.widget.TextView;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.BasicUserInfoBean;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;


/**
 * @ProjectName: ayoka7.
 * @ClassName: CourseClassroomMemberAdapter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/8/6 21:15.
 */

public class MemberListAdapter extends BaseQuickAdapter<BasicUserInfoBean, BaseViewHolder> {

    public static final int SINGLE_CHOICE = 1;
    public static final int MULTI_CHOICE = 2;

    private int choiceMode;

    public MemberListAdapter(int choiceMode) {
        super(R.layout.item_member_list, null);
        this.choiceMode = choiceMode;
    }

    @Override
    protected void convert(BaseViewHolder holder, BasicUserInfoBean item) {
        ImageLoader.getInstance().displayImage(item.LocalAvatar, (ImageView) holder.getView(R.id.iv_header), ImageLoaderOptions.getCircleOptions());
        TextView tvName = holder.getView(R.id.tv_name);
        tvName.setText(item.WeixinNickName);
        tvName.setSelected(item.isSelected);
        if (choiceMode == MULTI_CHOICE) {
            tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.selector_check_box, 0);
        } else if (choiceMode == SINGLE_CHOICE) {
            tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.selector_radio_button, 0);
        }
    }

    private int previousSelectedIndex;

    public void setSingleChoice(int position) {
        getData().get(position).isSelected = true;
        notifyItemChanged(position);
        if (position != previousSelectedIndex) {
            getData().get(previousSelectedIndex).isSelected = false;
            notifyItemChanged(previousSelectedIndex);
        }
        previousSelectedIndex = position;
    }

    public void setMultiChoice(int position) {
        boolean isSelected = getItem(position).isSelected;
        getItem(position).isSelected = !isSelected;
        notifyDataSetChanged();
    }
}
